﻿class Solution {//划动窗口
    public int[][] merge(int[][] intervals) {
      Arrays.sort(intervals,Comparator.comparingInt(interval->interval[0])) ;
      int n=intervals.length;
      int i=0;
      List<int[]> list=new ArrayList<>(n);
      while(i<n){
        int start=intervals[i][0];
        int end=intervals[i][1];
        int j=i+1;
        while(j<n&&intervals[j][0]<=end){
        end=Math.max(end,intervals[j++][1]);//更新数组末尾
        }
        list.add(new int[]{start,end});
        i=j;//更新i
      }
      return list.toArray(new int[list.size()][2]);
    }
}